package com.wanxing.sellerapp.emqx.handlers

import com.alibaba.fastjson.JSONObject
import com.blankj.utilcode.util.ShellUtils
import com.blankj.utilcode.util.ThreadUtils
import com.wanxing.sellerapp.App
import com.wanxing.sellerapp.audio.AudioHelper
import com.wanxing.sellerapp.config.AppConfig
import com.wanxing.sellerapp.emqx.entity.EmqPayload
import com.wanxing.sellerapp.emqx.interfaces.IMessenger
import com.wanxing.sellerapp.seller.SellerManager

/**
 * 处理程序配置
 */
class ConfigHandler(messenger: IMessenger, sellerManager: SellerManager) : AbstractActionHandler(messenger, sellerManager) {

    override fun handleAction(messageId: String?, data: JSONObject, payload: EmqPayload) {

        val serviceBaseUrl = data.getString("serviceBaseUrl")
        val brokerUrl = data.getString("brokerUrl")
        val channel = data.getString("channel")
        val runAdvApp = data.getBoolean("runAdvApp")

        val brokerChange = brokerUrl != AppConfig.brokerUrl
        val serviceChange = serviceBaseUrl != AppConfig.DEFAULT_SERVICE_URL
        val channelChange = channel != AppConfig.channel


        AppConfig.brokerUrl = brokerUrl
        AppConfig.channel = channel
        AppConfig.runAdvAppOnStart = runAdvApp

        val seller = sellerManager.seller

        // 修改配置后重启程序
         // 关闭广告程序
        if (brokerChange || serviceChange || channelChange) {
            ThreadUtils.runOnUiThreadDelayed({
                ShellUtils.execCmd("reboot", true, false)
            }, 200)
        }

    }

}